package com.scau.hwadee.examination.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scau.hwadee.examination.entity.Examplanapproval;
import com.scau.hwadee.examination.entity.Examsession;
import com.scau.hwadee.examination.service.ExaminationPlanService;
import com.scau.hwadee.examination.utils.JsonUtils;
import com.scau.hwadee.examination.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author YHX
 * @date 2019/8/1 15:57
 * description
 */
@Slf4j
@Controller
@RequestMapping("examination/plan")
@Api(tags = "ExaminationPlanController", description = "考试计划管理模块")
public class ExaminationPlanController {

  @Autowired
  private ExaminationPlanService examinationPlanService;

  @GetMapping("allExamSessionByState")
  @ApiOperation(value = "获取所有特定状态的考次")
  public ResponseEntity<ResponseVo> getAllDisableExamSession(@RequestParam("pageNum") int pageNum,
                                                             @RequestParam("pageSize") int pageSize,
                                                             @RequestParam("state") Integer state) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("获取所有特定状态的考次");
    vo.setData(examinationPlanService.getAllPlanStateExamSession(pageNum, pageSize, state));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("allNormalMajorByExamSession")
  @ApiOperation(value = "选择某专业版本的全部正常专业")
  public ResponseEntity<ResponseVo> getAllNormalMajorByExamSession(@RequestParam("pageNum") int pageNum,
                                                                   @RequestParam("pageSize") int pageSize,
                                                                   @RequestParam("majorVersionId") String majorVersionId,
                                                                   @RequestParam("state") String state) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("选择某专业版本的全部正常专业");
    vo.setData(examinationPlanService.getAllMajorByMajorPlanVersionAndMajorState(pageNum, pageSize, majorVersionId, state));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("newExammajor")
  @ApiOperation(value = "添加开考专业")
  @ApiImplicitParam(name = "param", value = "Json数组", dataType = "JsonArray", required = true)
  public ResponseEntity<ResponseVo> createExammajorList(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    JSONArray jsonArray = Objects.requireNonNull(JsonUtils.JSONToObject(param, JSONObject.class)).getJSONArray("jsonArray");
    int cnt = examinationPlanService.createOpeningExamMajor(jsonArray);
    String message;
    if (cnt > 0) {
      message = "添加开考专业成功";
    } else {
      message = "添加开考专业失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @DeleteMapping("examMajorList")
  @ApiOperation(value = "删除一个或者多个专业")
  @ApiImplicitParam(name = "param", value = "majorID的Json数组", dataType = "JsonArray", required = true)
  public ResponseEntity<ResponseVo> deleteExamMajorList(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    JSONArray jsonArray = Objects.requireNonNull(JsonUtils.JSONToObject(param, JSONObject.class)).getJSONArray("jsonArray");
    int cnt = examinationPlanService.deleteExamMajorList(jsonArray);
    String message;
    if (cnt > 0) {
      message = "删除开考专业成功";
    } else {
      message = "删除开考专业失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("allExamSessionWithoutCourse")
  @ApiOperation(value = "获取所有没有开考课程的考次")
  public ResponseEntity<ResponseVo> getAllExamSessionWithoutCourse(@RequestParam("pageNum") int pageNum,
                                                                   @RequestParam("pageSize") int pageSize) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("获取所有没有开考课程的考次");
    vo.setData(examinationPlanService.getAllExamSessionWithoutCourse(pageNum, pageSize));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("newExamCourses")
  @ApiOperation(value = "新增开考课程列表")
  public ResponseEntity<ResponseVo> addExamCourses(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("新增开考课程列表");
    log.debug("param is " + param);
    JSONArray jsonArray = Objects.requireNonNull(JsonUtils.JSONToObject(param, JSONObject.class)).getJSONArray("jsonArray");
    int cnt = examinationPlanService.createExamCourses(jsonArray);
    String message;
    if (cnt > 0) {
      message = "新增开考课程列表成功";
    } else {
      message = "新增开考课程列表失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("allExamCourseByExamSessionId")
  @ApiOperation(value = "根据考次ID获取所有的开考课程")
  public ResponseEntity<ResponseVo> getAllExamCourseByExamSessionId(@RequestParam("pageNum") int pageNum,
                                                                    @RequestParam("pageSize") int pageSize,
                                                                    @RequestParam("esId") String esId) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("根据考次ID获取所有的开考课程");
    vo.setData(examinationPlanService.getAllExamCourseByExamSessionId(pageNum, pageSize, esId));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("examTimeOfCourse")
  @ApiOperation(value = "设置考试日期和时间")
  public ResponseEntity<ResponseVo> setExamTimeOfCourse(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("设置考试日期和时间");
    log.debug("param is " + param);
    JSONArray jsonArray = Objects.requireNonNull(JsonUtils.JSONToObject(param, JSONObject.class)).getJSONArray("jsonArray");
    int cnt = examinationPlanService.updateExamCourseTime(jsonArray);
    String message;
    if (cnt > 0) {
      message = "设置考试日期和时间成功";
    } else {
      message = "设置考试日期和时间失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("reviewExamPlan")
  @ApiOperation(value = "审批考试计划")
  public ResponseEntity<ResponseVo> reviewExamPlan(@RequestBody String params) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("审批考试计划");
    Examplanapproval examplanapproval = JsonUtils.JSONToObject(params, Examplanapproval.class);
    int cnt = examinationPlanService.updateExamPlan(examplanapproval);
    String message;
    if (cnt > 0) {
      message = "审批专业计划成功";
    } else {
      message = "审批专业计划失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("allExamPlan")
  @ApiOperation(value = "获取所有的考试计划审批信息")
  public ResponseEntity<ResponseVo> getAllExamPlan(@RequestParam("pageNum") int pageNum,
                                                   @RequestParam("pageSize") int pageSize) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("获取所有的考试计划审批信息");
    vo.setData(examinationPlanService.getAllExamplanapprovals(pageNum, pageSize));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("examCourseSummary")
  @ApiOperation(value = "获取考试课程简表信息")
  public ResponseEntity<ResponseVo> getExamCourseSummary(@RequestParam("pageNum") int pageNum,
                                                         @RequestParam("pageSize") int pageSize,
                                                         @RequestParam("examSessionId") String examSessionId) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("获取考试课程简表信息");
    vo.setData(examinationPlanService.getExamCourseSummary(pageNum, pageSize, examSessionId));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("examOpeningCourseSummary")
  @ApiOperation(value = "获取考试开考课程简表")
  public ResponseEntity<ResponseVo> getExamOpeningCourseSummary(@RequestParam("pageNum") int pageNum,
                                                                @RequestParam("pageSize") int pageSize,
                                                                @RequestParam("examSessionId") String examSessionId) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("获取考试开考课程简表");
    vo.setData(examinationPlanService.getExamOpeningCourseSummary(pageNum, pageSize, examSessionId));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("examsProfessionalCoursesStatistics")
  @ApiOperation("考试课表专业课程数统计")
  public ResponseEntity<ResponseVo> getExamsProfessionalCoursesStatistics(@RequestParam("examSessionId") String examSessionId) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("考试课表专业课程数统计");
    vo.setData(examinationPlanService.examinationScheduleProfessionalCourseStatistics(examSessionId));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("allExamSessionWithoutMajorPlan")
  @ApiOperation(value = "获取所有的没有专业版本的考次信息")
  public ResponseEntity<ResponseVo> getAllExamSessionWithoutMajorPlan(@RequestParam("pageNum") int pageNum,
                                                                      @RequestParam("pageSize") int pageSize) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("获取所有的没有专业版本的考次信息");
    vo.setData(examinationPlanService.getAllExamsessionWithoutMajorPlan(pageNum, pageSize));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PutMapping("examSessionMessage")
  @ApiOperation(value = "更新考次信息")
  public ResponseEntity<ResponseVo> updateExamSessionMessage(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("更新考次信息");
    Examsession examsession = JsonUtils.JSONToObject(param, Examsession.class);
    int cnt = examinationPlanService.updateExamsession(examsession);
    String message;
    if (cnt > 0) {
      message = "更新考次信息成功";
    } else {
      message = "更新考次信息失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("allMajorsOfExamSessionId/{esId}")
  @ApiOperation(value = "获取某一考次的所有专业信息")
  public ResponseEntity<ResponseVo> getAllMajorsOfExamSessionId(@RequestParam("pageNum") int pageNum,
                                                                @RequestParam("pageSize") int pageSize,
                                                                @PathVariable("esId") String esId) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("获取某一考次的所有专业信息");
    vo.setData(examinationPlanService.getMajorsByMajorIds(pageNum, pageSize, esId));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @DeleteMapping("examCourses")
  @ApiOperation(value = "删除开考课程列表")
  public ResponseEntity<ResponseVo> deleteExamCoursesList(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("删除开考课程列表");
    JSONArray jsonArray = Objects.requireNonNull(JsonUtils.JSONToObject(param, JSONObject.class)).getJSONArray("jsonArray");
    String message;
    int cnt = examinationPlanService.deleteExamCourseList(jsonArray);
    if (cnt > 0) {
      message = "删除开考课程列表成功";
    } else {
      message = "删除开考课程列表失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }
}
